package google.sites.model.commands.webpage;

import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.sites.WebPageEntry;
import com.google.gdata.util.ServiceException;
import dev.tools.design.patterns.command.CommandException;
import google.sites.model.Model;
import google.sites.model.commands.SitesCommand;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: Nuno
 * Date: 02-10-2013
 * Time: 18:15
 */
public class RenameWebPage extends SitesCommand {
    private String newTitle;
    private WebPageEntry entry;

    public RenameWebPage(WebPageEntry entry) {
        this.entry = entry;
    }


    @Override
    protected void protectedExecute() throws CommandException {

        try {
            entry.setTitle(new PlainTextConstruct(newTitle));
            setResult(entry.update());
        } catch (Model.Exception | IOException | ServiceException e) {
            throw new CommandException(e);
        }
    }

    public RenameWebPage setNewTitle(String newTitle) {
        this.newTitle = newTitle;
        return this;
    }
}
